package com.okoj.excel_lib_rary.data.net;

import android.util.Log;

import com.okoj.excel_lib_rary.config.Config;
import com.okoj.excel_lib_rary.data.WpsService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class WpsClient {

    private static Config appConfig;

    private static String TAG = "WpsClient";

    private WpsClient(){}

    private static WpsClient wpsClient;

    private WpsService wpsService;

    private static final String baseUrl = "http://wps.shineyie.com/v1/3rd/";

    public static WpsClient getInstance(){
        return wpsClient;
    }

    public static void init(Config config) {
        if(wpsClient == null){
            wpsClient = new WpsClient();
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .build();
           wpsClient.wpsService =  new Retrofit.Builder()
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .baseUrl(baseUrl)
                    .build().create(WpsService.class);
        }
        appConfig = config;
    }

    public WpsService wpsService(){
        if(wpsClient == null || wpsService == null || appConfig == null){
            Log.e(TAG,"请初始化我");
            return null;
        }
        return wpsService;
    }

    public static Config getAppConfig() {
        return appConfig;
    }
}
